package com.zly.bscloud.service.impl;

import com.zly.bscloud.dto.ArticleDTO;
import com.zly.bscloud.entity.Article;
import com.zly.bscloud.mapper.ArticleMapper;
import com.zly.bscloud.service.ArticleService;
import com.zly.bscloud.utils.entity.StatusCode;
import com.zly.bscloud.utils.exception.BpException;
import com.zly.bscloud.utils.utils.BeanHelper;
import com.zly.bscloud.utils.utils.LuceneUtils;
import com.zly.bscloud.utils.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.zly.bscloud.utils.utils.LuceneUtils.multiFieldQueryParserTest;

/**
 * 用户service层实现类
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 插入一篇文章
     * @param map 其中包含文章的标题,简介,内容
     * @return 返回修改过的dto对象
     */
    @Override
    @Transactional
    public ArticleDTO saveArticle(Map<String, String> map){
        //System.out.println(map);
        // 首先新增文章
        Article article = new Article();
        article.setId(UUIDUtils.UUID());
        article.setCreate_time(new Date());
        article.setTitle(map.get("title"));
        article.setSynopsis(map.get("synopsis"));
        article.setContent(map.get("content"));
        article.setTags(map.get("tags"));
        article.setIslikes(0);
        article.setViews(0);
        article.setMessages(0);
        article.setType(map.get("type"));
        article.setFirst_picture(map.get("first_picture"));

        // 保存进数据库
        int count = articleMapper.insert(article);
        try{
            LuceneUtils.indexWriterTest(article);
        }catch (Exception e){

        }

        if (count != 1)
            throw new BpException(StatusCode.SaveArticleError);

        ArticleDTO articleDTO = BeanHelper.copyProperties(article, ArticleDTO.class);
        return articleDTO;
    }

    /**
     * 获得全部文章的dto信息
     * @return 返回所有文章的dto信息
     */
    @Override
    public List<ArticleDTO> getAllArticles() {
        //System.out.println(1111);
        // 创建Example
        //Example example = new Example(Article.class);
        // 创建Criteria
        //Example.Criteria criteria = example.createCriteria();
        // 添加条件
        //criteria.andEqualTo("received", 0);
        //System.out.println(example);
        List<Article> articleList = articleMapper.selectAll();
        //System.out.println(articleList);
        List<ArticleDTO> articleDTOList = BeanHelper.copyWithCollection(articleList, ArticleDTO.class);
        return articleDTOList;
    }

    /**
     * 通过文章id获取到一个文章的信息
     * @param id 文章id
     * @return 返回文章的dto信息
     */
    @Override
    @Transactional
    public ArticleDTO getArticleByAId(String id){
        Article article = articleMapper.selectByPrimaryKey(id);
        //System.out.println(article);
        article.setViews(article.getViews()+1);
        articleMapper.updateByPrimaryKey(article);
        ArticleDTO articleDTO = BeanHelper.copyProperties(article, ArticleDTO.class);
        return articleDTO;
    }

    /**
     * 通过文章的id获取文章的内容
     * @param Id 文章的id
     * @return 返回文章的内容
     */
    @Override
    public String getContentByAId(String Id) {
        // 首先查找此文章
        Article article = articleMapper.selectByPrimaryKey(Id);
        // 获取文章内容
        String content = article.getContent();
        return content;
    }

    /**
     * 通过key获取到文章的信息
     * @param key 关键字
     * @return 返回文章的dto信息
     */
    @Override
    @Transactional
    public List<ArticleDTO> getArticleByKey(String key){
        System.out.println(key);
        List<Article> articleList = new ArrayList<>();
        try{
            LuceneUtils.setUp();
            articleList = multiFieldQueryParserTest(key);
            LuceneUtils.tearDown();
        }catch (Exception e){

        }

        List<ArticleDTO> articleDTOList = BeanHelper.copyWithCollection(articleList, ArticleDTO.class);
        return articleDTOList;
    }
}
